﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Arbalest.Models;
using System.Web.Mvc;

namespace Arbalest.Web.Services
{
    public class CompanyService : ICompanyService
    {
        private IModelStateService ModelStateService { get; set; }
        private ICompanyRepository CompanyRepository { get; set; }

        #region constructors
        
        public CompanyService(IModelStateService modelState, ICompanyRepository repository)
        {
            this.ModelStateService = modelState;
            this.CompanyRepository = repository;
        }

        public CompanyService(ModelStateDictionary modelStateDictionary, ICompanyRepository repository)
            : this(new ModelStateService(modelStateDictionary), repository)
        {
        }

        public CompanyService()
            : this(new ModelStateDictionary(), new CompanyRepository())
        {
        }

        #endregion constructors

        public bool Validate(Company model)
        {
            // TODO: Add validation rules here
            //if (workToValidate.Name.Trim().Length == 0)
            //    _validatonDictionary.AddModelError("Name", "Name is required.");

            //if (workToValidate.Description.Trim().Length == 0)
            //    _validatonDictionary.AddModelError("Description", "Description is required.");

            //if (workToValidate.UnitsInStock < 0)
            //    _validatonDictionary.AddModelError("UnitsInStock", "Units in stock cannot be less than zero.");

            if (string.IsNullOrEmpty(model.Name.Trim()))
                this.ModelStateService.AddError("UnitsInStock", "Units in stock cannot be less than zero.");

            return this.ModelStateService.IsValid;
        }

        #region ICompanyServiceContract Members

        public IEnumerable<Company> List()
        {
            return this.CompanyRepository.List();
        }

        public Company Get(long id)
        {
            return this.CompanyRepository.Get(id);
        }

        #endregion
    }
}